<?php

namespace IDock\Insurance\ZFIC;

use Exception;
use IDock\Http\Response;
use IDock\IDock\IDockInterface;
use IDock\Insurance\ACIC\Response\VehicleActualValueResponse;
use IDock\Insurance\ZFIC\Request\CancelTradeRequest;
use IDock\Insurance\ZFIC\Request\CarModelQueryBXRequest;
use IDock\Insurance\ZFIC\Request\CarModelQueryRequest;
use IDock\Insurance\ZFIC\Request\GetCarVerifyCodeRequest;
use IDock\Insurance\ZFIC\Request\GetOrderNoRequest;
use IDock\Insurance\ZFIC\Request\GetPaymentUrlRequest;
use IDock\Insurance\ZFIC\Request\InsuranceConfirmRequest;
use IDock\Insurance\ZFIC\Request\NonCarProductListRequest;
use IDock\Insurance\ZFIC\Request\OfferOrderPriceRequest;
use IDock\Insurance\ZFIC\Request\OrderCancelRequest;
use IDock\Insurance\ZFIC\Request\OrderPriceQueryRequest;
use IDock\Insurance\ZFIC\Request\OrderStatusQueryRequest;
use IDock\Insurance\ZFIC\Request\RenewalInquiryRequest;
use IDock\Insurance\ZFIC\Request\UploadFileRequest;
use IDock\Insurance\ZFIC\Request\ValidateCarRequest;
use IDock\Insurance\ZFIC\Response\CancelOrderResponse;
use IDock\Insurance\ZFIC\Response\CancelTradeResponse;
use IDock\Insurance\ZFIC\Response\CommonResponse;
use IDock\Insurance\ZFIC\Response\GetOrderNoResponse;
use IDock\Insurance\ZFIC\Response\InsuranceConfirmResponse;
use IDock\Insurance\ZFIC\Response\InsurancePriceQueryResponse;
use IDock\Insurance\ZFIC\Response\NonCarProductListResponse;
use IDock\Insurance\ZFIC\Response\OrderStatusQueryResponse;
use IDock\Insurance\ZFIC\Response\PolicyPaymentResponse;
use IDock\Insurance\ZFIC\Response\RenewalInquiryResponse;
use IDock\Insurance\ZFIC\Response\ValidateCarResponse;
use IDock\Insurance\ZFIC\Response\VehicleModelBXResponse;
use IDock\Insurance\ZFIC\Response\VehicleModelResponse;
use IDock\Printer\NonCar\NonCarInquiryPrinter;
use IDock\Printer\NonCar\NonCarInquirySinglePrinter;
use IDock\Printer\Premium\PremiumInquiryPrinter;
use IDock\Printer\PremiumUnderwritingPrinter;
use IDock\Printer\SpecialClausesPrinter;
use IDock\Printer\VehicleActualValuePrinter;
use IDock\Visitor\EPolicyDownloadVisitor;
use IDock\Visitor\NonCar\NonCarInquiryVisitor;
use IDock\Visitor\NonCar\NonCarProductListVisitor;
use IDock\Visitor\NonCar\NonCarProductVisitor;
use IDock\Visitor\PolicyInfoVisitor;
use IDock\Visitor\PolicyPaymentVisitor;
use IDock\Visitor\Premium\PremiumInquiryVisitor;
use IDock\Visitor\ProposalRecallVisitor;
use IDock\Visitor\ProposalStatusVisitor;
use IDock\Visitor\RenewalInquiryVisitor;
use IDock\Visitor\UnderwritingRecallVisitor;
use IDock\Visitor\UploadFileVisitor;
use IDock\Visitor\ValidateCarVisitor;
use IDock\Visitor\VehicleActualValueVisitor;
use IDock\Visitor\VehicleModelVisitor;

/**
 * 珠峰保险
 */
class ZFICConstruct extends Builder implements IDockInterface
{

    protected $config = [];

    public function __construct($config)
    {
        $this->config = $config;
        $this->chalCode = $config['chalCode'] ?? '';
        $this->userCode = $config['userCode'] ?? '';
        $this->productCode = $config['productCode'] ?? '';
        $this->planCode = $config['planCode'] ?? '';
        $this->planCodeNew = $config['planCodeNew'] ?? '';
    }

    /**
     * 车型查询
     * @param VehicleModelVisitor $vehicleModelVisitor
     * @return VehicleModelResponse
     */
    public function vehicleModel2(VehicleModelVisitor $vehicleModelVisitor)
    {
        $totalLst = [];
        //先请求保信接口
        $request = new CarModelQueryBXRequest();
        $request->setVisitor($vehicleModelVisitor);
        /*** @var VehicleModelBXResponse $response */
        $response = $this->execute($request);

        if (!$response->isSuccess() or empty($response->printer->toArray())) {
            $request = new CarModelQueryRequest();
            $request->setVisitor($vehicleModelVisitor);
            /*** @var VehicleModelResponse $response */
            $response = $this->execute($request);
        }

        if ($response->isSuccess() and $response->printer->toArray()) {
            $totalLst = array_merge($totalLst,$response->printer->toArray());
        }

        $response->printer->setModelListLst($totalLst);

        return $response;
    }

    public function vehicleModel(VehicleModelVisitor $vehicleModelVisitor)
    {
        $request = new CarModelQueryBXRequest();
        $request->setVisitor($vehicleModelVisitor);
        /*** @var VehicleModelBXResponse $response */
        $response = $this->execute($request);
        if (!$response->isSuccess() or empty($response->printer->toArray())) {
            $request = new CarModelQueryRequest();
            $request->setVisitor($vehicleModelVisitor);
            /*** @var VehicleModelResponse $response */
            $response = $this->execute($request);
            if ($response->totalCount > 10) {
                $request->totalCount = $response->totalCount;
                /*** @var VehicleModelResponse $response */
                $response = $this->execute($request);
            }
        }

         return $response;
     }

    /**
     * 续保查询
     * @param RenewalInquiryVisitor $renewalInquiryVisitor
     * @return RenewalInquiryResponse
     * @version 4.0
     */
    public function renewalInquiry(RenewalInquiryVisitor $renewalInquiryVisitor): RenewalInquiryResponse
    {
        $request = new RenewalInquiryRequest();
        $request->setVisitor($renewalInquiryVisitor);

        /*** @var RenewalInquiryResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    /**
     * 车辆实际价格接口
     * @param VehicleActualValueVisitor $vehicleActualValueVisitor
     * @return VehicleActualValueResponse
     */
    public function vehicleActualValue(VehicleActualValueVisitor $vehicleActualValueVisitor): VehicleActualValueResponse
    {

        $response = new VehicleActualValueResponse();
        $response->setCode(200);
        $response->setVisitor($vehicleActualValueVisitor);
        $response->printerInit();
        return $response;

        //$response = new CommonResponse();
        //$response->setCode('200');
        //$vehicleActualValuePrinter = new VehicleActualValuePrinter();
        //$response->printer = $vehicleActualValuePrinter;
        //return $response;
    }

    /**
     * 询价前置
     * @param PremiumInquiryVisitor $premiumInquiryVisitor
     */
    public function preInquiry(PremiumInquiryVisitor $premiumInquiryVisitor)
    {
    }

    public function validateCar(PremiumInquiryVisitor $premiumInquiryVisitor)
    {
    }

    /**
     * 询价
     * @param PremiumInquiryVisitor $premiumInquiryVisitor
     * @return InsurancePriceQueryResponse
     * @throws Exception
     */
    public function premiumInquiry(PremiumInquiryVisitor $premiumInquiryVisitor): InsurancePriceQueryResponse
    {
        $request = new OrderPriceQueryRequest();
        $request->setVisitor($premiumInquiryVisitor);

        /*** @var $response CommonResponse */
        $response = $this->execute($request);

        if ($response->getLogicCode() != '200') {
            throw new Exception($response->getMessage() ? $response->getMessage() : $response->getError());
        }
        $data = $response->getData();

        $actualPrice = $data['carInfo']['actualValue'] ?? '';
        $platOrderNo = $data['platOrderNo'] ?? '';
        $carClauseVMList = $data['carClauseVMList'] ?? [];

        $premiumInquiryVisitor->getVehicleVisitor()->actualValue = $actualPrice;//实际价格
        $premiumInquiryVisitor->getExtInfoVisitor()->argc['platOrderNo'] = $platOrderNo;

        $coverageList = $data['kindList'] ?? [];
        $engageList = $data['carClauseVMList'] ?? [];

        $request1 = new OfferOrderPriceRequest();
        $request1->setVisitor($premiumInquiryVisitor);
        $request1->setCoverageList($coverageList);

        /*** @var InsurancePriceQueryResponse $response */
        $response = $this->execute($request1);
        /** @var PremiumInquiryPrinter $printer **/
        $printer = $response->getPrinter();
        $printer->argc['isNewCar'] = $premiumInquiryVisitor->getVehicleVisitor()->isNewVehicle;
        
        //特约
        foreach ($engageList as $val) {
            $engage = new SpecialClausesPrinter();
            $engage->clauseName = $val['clauseName'];
            $engage->clauseCode = $val['clauseCode'];
            $engage->clauseContent = $val['context'];
            $engage->autoFlag = $val['needFlag'] == 'Y' ? '1' : '0';
            $engage->riskType = $val['riskCode'] == '0801' ? 'TCI' : 'VCI';//交强险可选特约
            $printer->specialClausesAppend($engage);
        }
        //业务等级
        if ($printer->premiumFactorPrinter) {
            $printer->premiumFactorPrinter->ComFactorAppend('业务等级', 'businessLevel', $premiumInquiryVisitor->getExtInfoVisitor()->argc['businessLevel']);
        }

        //非车结果回填
        $nonCarInquiryPrinter = new NonCarInquiryPrinter();
        $totalPremium = 0;
        foreach ($premiumInquiryVisitor->getNonCarInquiryVisitor()->getNonCarList() as $item) {
            /*** @var $item NonCarProductVisitor */
            $single = new NonCarInquirySinglePrinter();
            $single->premium = $item->premium;
            $single->planCode = $item->planCode;
            $single->productCode = $item->productCode;
            $single->statusCode = 'success';
            $totalPremium += $single->premium * $item->insuredNum;
            $nonCarInquiryPrinter->noCarInquirySingleAppend($single);
        }
        $nonCarInquiryPrinter->totalPremium = $totalPremium;

        $printer->nonCarInquiryPrinter = $nonCarInquiryPrinter;

        $response->printer = $printer;
        return $response;
    }

    /**
     * 核保
     * @param PremiumInquiryVisitor $premiumInquiryVisitor
     * @return InsuranceConfirmResponse
     */
    public function underwriting(PremiumInquiryVisitor $premiumInquiryVisitor): InsuranceConfirmResponse
    {
        $request = new InsuranceConfirmRequest();
        $request->setVisitor($premiumInquiryVisitor);

        /**@var InsuranceConfirmResponse $response * */
        $response = $this->execute($request);

        //非车数据回填重构
        /*** @var PremiumUnderwritingPrinter $printer */
        $printer = $response->getPrinter();
        if ($printer->noCarUnderwritingPrinter->statusCode == 'success' && !empty($premiumInquiryVisitor->getNonCarUnderwritingVisitor()->getNonCarList())) {

            /*** @var NonCarProductVisitor $nonProduct */
            $nonProduct = $premiumInquiryVisitor->getNonCarUnderwritingVisitor()->getNonCarList()[0];

            $nonCarList = $printer->noCarUnderwritingPrinter->getNonCarInquiryList();

            foreach ($nonCarList as $value) {
                /*** @var NonCarInquirySinglePrinter $value */
                $value->productCode = $nonProduct->productCode;
                $value->planCode = $nonProduct->planCode;
            }
        }

        $response->printer = $printer;
        return $response;
    }

    /**
     * 获取支付链接
     * @param PolicyPaymentVisitor $paymentVisitor
     * @return PolicyPaymentResponse
     */
    public function policyPayment(PolicyPaymentVisitor $paymentVisitor): PolicyPaymentResponse
    {

        $request1 = new GetCarVerifyCodeRequest();
        $request1->setVisitor($paymentVisitor);


        $response1 = $this->execute($request1);

        $code = $response1->getData();
//        $code = 'ZF17Y0';

        $paymentVisitor->argc['ZFCode'] = $code ?? '';

        $request = new GetPaymentUrlRequest();
        $request->setVisitor($paymentVisitor);

        /**@var PolicyPaymentResponse $response * */
        $response = $this->execute($request);

        return $response;
    }

    /**
     * 投保单状态
     * @param ProposalStatusVisitor $proposalStatusVisitor
     * @return OrderStatusQueryResponse
     */
    public function proposalStatus(ProposalStatusVisitor $proposalStatusVisitor): OrderStatusQueryResponse
    {
        $request = new OrderStatusQueryRequest();
        $request->setVisitor($proposalStatusVisitor);

        /**
         * @var OrderStatusQueryResponse $response
         */
        $response = $this->execute($request);
        return $response;
    }

    /**
     * 核保撤单
     * @param ProposalRecallVisitor $revokeVisitor
     * @return CancelOrderResponse
     */
    public function proposalRecall(ProposalRecallVisitor $revokeVisitor): Response
    {
        //见费出单撤销
        //if ($revokeVisitor->hasPayCode) {
        //    //获取交易流水号
        //    $getOrderNoRequest = new GetOrderNoRequest();
        //    $getOrderNoRequest->setVisitor($revokeVisitor);
        //    /***@var getOrderNoResponse $getOrderNoResponse */
        //    $getOrderNoResponse = $this->execute($getOrderNoRequest);
        //
        //    if (!$getOrderNoResponse->isSuccess()) {
        //        return $getOrderNoResponse;
        //    }
        //    //交易流水号撤销接口
        //    $cancelTradeRequest = new CancelTradeRequest();
        //    $cancelTradeRequest->setVisitor($revokeVisitor);
        //    $cancelTradeRequest->tradeNo = $getOrderNoResponse->tradeNo;
        //
        //    /***@var CancelTradeResponse $cancelTradeResponse */
        //    $cancelTradeResponse = $this->execute($getOrderNoRequest);
        //
        //    if (!$cancelTradeResponse->isSuccess()) {
        //        return $cancelTradeResponse;
        //    }
        //}

        $request = new OrderCancelRequest();
        $request->setVisitor($revokeVisitor);

        /*** @var CancelOrderResponse $response */
        $response = $this->execute($request);
        return $response;
    }

    /**
     * 人工核保撤单
     * @param UnderwritingRecallVisitor $recallVisitor
     * @return CancelOrderResponse
     */
    public function underwritingRecall(UnderwritingRecallVisitor $recallVisitor): CancelOrderResponse
    {

        $request = new OrderCancelRequest();
        $request->setVisitor($recallVisitor);

        /**
         * @var CancelOrderResponse $response
         */
        $response = $this->execute($request);
        return $response;
    }

    public function EPolicyDownload(EPolicyDownloadVisitor $EPolicyVisitor)
    {
    }

    public function policyInfo(PolicyInfoVisitor $policyInfoVisitor): OrderStatusQueryResponse
    {
        $request = new OrderStatusQueryRequest();
        $request->setVisitor($policyInfoVisitor);

        /**
         * @var OrderStatusQueryResponse $response
         */
        $response = $this->execute($request);
        return $response;
    }

    public function UploadFile(UploadFileVisitor $uploadFileVisitor)
    {
        $request = new UploadFileRequest();
        $list = $uploadFileVisitor->fileList;
        if (empty($list)) {
            throw new Exception("图片不能为空");
        }
        $response = new CommonResponse();
        foreach ($list as $val) {
            $visitor = new UploadFileVisitor();
            $visitor->argc = $uploadFileVisitor->argc;
            $visitor->ciInquiryNo = $uploadFileVisitor->ciInquiryNo;
            $visitor->biInquiryNo = $uploadFileVisitor->biInquiryNo;
            $visitor->fileAppend($val['type'], $val['url'], $val['base64']);
            $request->setVisitor($visitor);
            $response = $this->execute($request);
            usleep(500);
        }
        return $response;
    }

    /**
     * 非车产品列表
     * @param NonCarProductListVisitor $nonCarProductListVisitor
     * @return NonCarProductListResponse
     * @version 4.0
     */
    public function nonCarProductList(NonCarProductListVisitor $nonCarProductListVisitor): NonCarProductListResponse
    {
        $request = new NonCarProductListRequest();
        $request->setVisitor($nonCarProductListVisitor);

        /*** @var $response NonCarProductListResponse */
        $response = $this->execute($request);

        return $response;
    }

    public function nonCarInquiry(NonCarInquiryVisitor $nonCarInquiryVisitor)
    {
    }

    public function nonCarUnderwriting(NonCarInquiryVisitor $inquiryPrinter)
    {
    }

    /**
     * 验车码
     * @param ValidateCarVisitor $validateCarVisitor
     * @return ValidateCarResponse
     */
    public function validateCarCode(ValidateCarVisitor $validateCarVisitor): ValidateCarResponse
    {
        $request = new ValidateCarRequest();
        $request->setVisitor($validateCarVisitor);

        /*** @var $response ValidateCarResponse */
        $response = $this->execute($request);
        return $response;
    }

    /**
     * 新车备案
     * @return void
     */
    public function newVehicleRegistration(PremiumInquiryVisitor $premiumInquiryVisitor)
    {
    }
}
